#
# fusion.http.Disco
#
# Copyright 2007 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Toni Ruottu <toni.ruottu@iki.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

'''
Handle URLs of the form "/disco".

Fusion-disco! - a webpage "stream" example

This is a webpage that makes use of xhtml, css and, most
importantly, modern web browsers' ability to render webpages
before they have been fully loaded.
'''

import web
import random

class Disco(object):

    def GET(self):

        web.output('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>fusion-disco! - version 0.1</title> </head> <body>')

        i = 0
        while i <= 10:
            i += 1
            x = random.randint( 0, 90)
            y = random.randint( 0, 90)
            w = random.randint( 0, 100 - x )
            h = random.randint( 0, 100 - y )
            
            r = random.randint( 0, 255)
            g = random.randint( 0, 255)
            b = random.randint( 0, 255)
            
            style = 'background-color: rgb( ' + str(r) + ', ' + str(g) + ', ' + str(b) + ' ); ' + 'position: absolute; ' + 'left: ' + str(x) + '%; ' + 'top: ' + str(y) + '%; ' + 'width: ' + str(w) + '%; ' + 'height: ' + str(h) + '%;'

            text = 'fusion-disco!'

            next = '<div style="' + style + '">' + text + '</div>'

            web.output(next)

        web.output( "</body> <!-- Let's close them. We don't want to send an invalid --> </html>   <!-- document if the while( true ) fails us. ;-)        --> ")

        return
